{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "b897cfe9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import json\n",
    "from Api_Key import*\n",
    "from PIL import Image\n",
    "import base64\n",
    "import io\n",
    "API_KEY=API_KET_FACE\n",
    "API_SECRET=API_Secret_FACE\n",
    "URL = 'https://api-cn.faceplusplus.com/facepp/v1/ageing'\n",
    "IMAGE_URL =r\"C:\\Users\\yly\\Pictures\\liu.jpg\"\n",
    "files = {\"image_file\": open(IMAGE_URL, \"rb\")}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "c217a4b8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 请求参数\n",
    "data = {\"api_key\":API_KEY, \n",
    "        \"api_secret\":API_SECRET \n",
    "       }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "5909df1f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 发送请求\n",
    "response = requests.post(URL,data=data,files=files)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "82f3e47e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<Response [403]>\n"
     ]
    }
   ],
   "source": [
    "print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "5935dff8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'error_message': 'CONCURRENCY_LIMIT_EXCEEDED'}\n"
     ]
    }
   ],
   "source": [
    "req_con = response.json()\n",
    "print(req_con)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "6363463d",
   "metadata": {},
   "outputs": [],
   "source": [
    "#获取美颜后的照片\n",
    "base64_str=req_con['result']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "366bfe6c",
   "metadata": {},
   "outputs": [],
   "source": [
    "image_data = base64.b64decode(base64_str)\n",
    "image = Image.open(io.BytesIO(image_data))\n",
    "image.save(\"output.png\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "74052a8b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "torch",
   "language": "python",
   "name": "torch"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
